import Vue from 'vue'
import VueRouter from 'vue-router'
import logIn from '../views/logIn.vue'
import Home from '../views/Home.vue'
Vue.use(VueRouter)

const routes = [
  // 創建登錄組件
  { path: '/', redirect: 'logIn' },
  { path: '/logIn', component: logIn },
  {
    path: '/Home', component: Home,
    redirect: '/welcome',
    children: [
      { path: '/welcome', component: () => import(/* webpackChunkName: "Welcome" */ '../components/Welcome.vue') },
      { path: '/users', component: () => import(/* webpackChunkName: "users" */ '../components/user/Users.vue') },
      { path: '/rights', component: () => import(/* webpackChunkName: "rights" */ '../components/power/Rights.vue') },
      { path: '/roles', component: () => import(/* webpackChunkName: "roles" */ '../components/power/Roles.vue') },
      { path: '/categories', component: () => import(/* webpackChunkName: "categories" */ '../components/goods/Cate.vue') },
      { path: '/params', component: () => import(/* webpackChunkName: "params" */ '../components/goods/Params.vue') },
      { path: '/goods', meta: { title: '商品列表' }, component: () => import(/* webpackChunkName: "goods" */ '../components/goods/GoodsList.vue') },
      { path: '/goods/add', meta: { title: '添加商品' }, component: () => import(/* webpackChunkName: "/goods/add" */ '../components/goods/add') },
      { path: '/orders', meta: { title: '订单列表' }, component: () => import(/* webpackChunkName: "orders" */ '../components/order/Order.vue') },
      { path: '/reports', meta: { title: '查看报表' }, component: () => import(/* webpackChunkName: "report" */ '../components/report/Report.vue') },
    ]
  }


]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
//  掛載路由守衛 設置訪問權限

router.beforeEach((to, from, next) => {
  // to 將要去哪  form 從哪裡來  next() / next('/home') 強制跳轉  / 走不走
  if (to.path === '/logIn') return next(); // 登錄直接放行
  // 不是登錄 看有沒得token,有放行 ,沒得強制去登錄頁
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/logIn')
  next()

})

export default router
